package com.example.boot.controller;

import com.example.boot.User;
import com.sun.media.sound.SoftTuning;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * restfull风格的命名;
 * get和post方法是html中自带的，但是不支持PUT和DELETE方法
 * 所以需要通过POST方法模拟这两种方法，只需要在表单中添加一个隐藏域，名为_method，值为PUT或DELETE;
 * example:
 * <form action="users" method="post">
 * <input type="hidden" name="_method" value="DELETE">
 * <input type="text" name="uid" value="4">
 * <input type="submit" value="发送DELETE请求">
 * </form>
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @PostMapping("/")
    public void addUser(@ModelAttribute User user) {
        System.out.println(user.getAge() + " " + user.getName());
        System.out.println("add");
    }


    @DeleteMapping("/{uid}")
    public void deleteUser(@PathVariable String uid) {
        System.out.println("del:" + uid);
    }

    @PutMapping("/{uid}")
    public void updateUser(@PathVariable String uid, @ModelAttribute User user) {
        System.out.println(user.getAge() + " " + user.getName());
    }

    @GetMapping("/")
    public List<User> getUsers() {
        User u = new User();
        u.setAge(23);
        u.setName("hi");
        List list = new ArrayList();
        list.add(u);
        return list;
    }


    @GetMapping("/{uid}")
    public User getUser(@PathVariable String uid) {
        System.out.println(uid);
        User u = new User();
        u.setAge(23);
        u.setName("hi");
        return u;
    }
}
